#include<bits/stdc++.h>
using namespace std;
#define int long long

int a[1005], preSum[1005];

signed main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		preSum[i] = preSum[i - 1] + a[i];
	}

	multiset<int> s;

	// 枚举右区间
	for (int i = 1; i <= n; i++)
		for (int j = i; j <= n; j++)
			s.insert(preSum[j] - preSum[i - 1]);


	int ans = INT_MAX;

	// 枚举 r1
	for (int r1 = 1; r1 <= n; r1++) {
		// 删除以 r1 作为左端点的右区间
		for (int r2 = r1; r2 <= n; r2++) {
			auto p = s.find(preSum[r2] - preSum[r1 - 1]);
			s.erase(p);
		}

		// 删除相交区间后，开始找答案
		for (int l1 = 1; l1 <= r1; l1++) {
			int target = preSum[r1] - preSum[l1 - 1];

			// 二分找值最接近的右区间
			auto p = s.lower_bound(target);
			if (p != s.end())
				ans = min(ans, abs(target - *p));
			if (p != s.begin())
				p--,
				ans = min(ans, abs(target - *p));
		}
	}
	cout << ans;
	return 0;
}
